/**
 * @(#) HealthCheckUpProxy.as
 *
 * @date		2009. 05. 07.
 * @version		1.0
 * @author		Park, Juneho
 * @description	 건강검진 Proxy - Proxy
 * 				건강검진 조회를한다.
 */
package com.skt.common_hr.healthcheckup.model
{
	import com.skt.common_hr.common.Variables;
	import com.skt.common_hr.common.puremvc.CommonProxy;
	import com.skt.common_hr.healthcheckup.HealthCheckUpFacade;
	import com.skt.common_hr.healthcheckup.model.vo.HealthchkreqVO;
	import com.skt.common_hr.healthcheckup.model.vo.HealthchkreqWriteVO;
	import com.skt.common_hr.healthcheckup.model.vo.HospitalDefaultListVO;
	import com.skt.common_hr.healthcheckup.model.vo.HospitalDefaultVO;
	import com.skt.common_hr.healthcheckup.model.vo.HospitalVO;
	import com.wcl.dataloader.DataLoaderFactory;
	import com.wcl.dataloader.IDataLoader;
	import com.wcl.utils.DateUtil;
	
	import mx.collections.ArrayCollection;
	import mx.managers.CursorManager;
	import mx.resources.ResourceManager;
	import mx.rpc.events.FaultEvent;
	import mx.rpc.events.ResultEvent;
		
	public class HealthCheckUpProxy extends CommonProxy
	{
		public static const NAME:String = "HealthCheckUpProxy";
		
		public var dataList:ArrayCollection;
		public var hospitalList:ArrayCollection;
		public var choiceCombo:ArrayCollection;
		public var hospitalDefault:ArrayCollection;
		public var hospitalDefaultList:ArrayCollection;
		public var monthData:ArrayCollection;
		public var message:String;
		
		
		/**
		 * 생성자
		 * 
		 * @param
		 * @return void
		 */
		public function HealthCheckUpProxy(data:Object=null)
		{
			super(NAME, data);
		}
		
		/**
		 * data load를 수행
		 * 		- DataLoader 를 생성하고 Import Parameter 객체를 생성, RFC를 호출
		 * 
		 * @param pernr 사번
		 * @return void
		 */
		public function load(pernr:String):void
		{
			var params:Object = new Object();
			params["I_PERNR"] = pernr;
			
			loadData("Z_HR_0094", params, loadResult, loadFault);
		}
		
		
		//병원목록
		public function hospitalLoad():void
		{
			var params:Object = new Object();
			
			loadData("Z_HR_0090", params, hospitalResult, hospitalFault);
		}
		
		//병원상세정보
		public function hospitalDefaultLoad(code:String):void
		{
			var params:Object = new Object();
			params["I_STCD2"] = code;
			
			loadData("Z_HR_0091", params, hospitaDefaultlResult, hospitaDefaultFault);
		}
		
		
		//저장
		public function save(peran:String, year:String, vo:HealthchkreqWriteVO):void
		{
			var params:Object = new Object();
			params["I_PERNR"] = peran;
			params["I_YEAR"]  = year;
			
			var tables:Object = new Object();
			var table:Array = new Array();
			
			table.push(vo);
			tables["ITAB"] = table;
			
			saveData("Z_HR_0095", params, tables, saveResult, saveFault);
		}
		
		
		///////////////////////////////////////////////////////////////////////////////////////////////////
		//
		//	Event Listeners
		//
		///////////////////////////////////////////////////////////////////////////////////////////////////
		/**
		 * 서버에서 정상적으로 리턴된 결과를 처리
		 * 
		 * @param
		 * @return void
		 */
		public function loadResult(event:ResultEvent):void
		{
			if(event.result["RETCODE"]==0) {  
				dataList = parseCongList(event.result.ITAB);
				parseOpinion(event.result.ITAB1);
				facade.sendNotification(HealthCheckUpFacade.LOAD_Healthchkreq_LIST_COMPLETE, this);
			}
			else {
				trace( "failed to load cong: (" +event.result["RETCODE"] + ") "+event.result["RETTEXT"]);
				facade.sendNotification(HealthCheckUpFacade.LOAD_Healthchkreq_LIST_FAILED, this);
			}
			CursorManager.removeBusyCursor();
		}
		
		/**
		 * 서버에서 오류가 발생했을 처리
		 * 
		 * @param
		 * @return void
		 */
		private function loadFault(event:FaultEvent):void
		{
			trace("Fault message in HealthCheckUpProxy: " + event.fault.faultString);
			facade.sendNotification(HealthCheckUpFacade.LOAD_Healthchkreq_LIST_FAILED, this);
			CursorManager.removeBusyCursor();
		}
		
	
		public function hospitalResult(event:ResultEvent):void
		{
			if(event.result["RETCODE"]==0) {  
				hospitalList = parseHospitalList(event.result.ITAB);
				facade.sendNotification(HealthCheckUpFacade.LOAD_HOSPITAL_LIST_COMPLETE, this);
			}
			else {
				trace( "failed to load cong: (" +event.result["RETCODE"] + ") "+event.result["RETTEXT"]);
				facade.sendNotification(HealthCheckUpFacade.LOAD_HOSPITAL_LIST_FAILED, this);
			}
			CursorManager.removeBusyCursor();
		}
		
		private function hospitalFault(event:FaultEvent):void
		{
			trace("Fault message in HealthCheckUpProxy: " + event.fault.faultString);
			facade.sendNotification(HealthCheckUpFacade.LOAD_HOSPITAL_LIST_FAILED, this);
			CursorManager.removeBusyCursor();
		}
		
		public function hospitaDefaultlResult(event:ResultEvent):void
		{
			if(event.result["RETCODE"]==0) {  
				hospitalDefault = parseHospitalDefault(event.result.ITAB1);
				hospitalDefaultList = parseHospitalDefaultList(event.result.ITAB2);
				facade.sendNotification(HealthCheckUpFacade.LOAD_HOSPITAL_DEFAULT_COMPLETE, this);
			}
			else {
				trace( "failed to load cong: (" +event.result["RETCODE"] + ") "+event.result["RETTEXT"]);
				facade.sendNotification(HealthCheckUpFacade.LOAD_HOSPITAL_DEFAULT_FAILED, this);
			}
			CursorManager.removeBusyCursor();
		}
		
		private function hospitaDefaultFault(event:FaultEvent):void
		{
			trace("Fault message in HealthCheckUpProxy: " + event.fault.faultString);
			facade.sendNotification(HealthCheckUpFacade.LOAD_HOSPITAL_DEFAULT_FAILED, this);
			CursorManager.removeBusyCursor();
		}
		
		public function choiceResult(event:ResultEvent):void
		{
			if(event.result["RETCODE"]==0) {  
				choiceCombo = parseChoiceCombo(event.result.ITAB);
				facade.sendNotification(HealthCheckUpFacade.LOAD_CHOICE_COMBO_LIST_COMPLETE, this);
			}
			else {
				trace( "failed to load cong: (" +event.result["RETCODE"] + ") "+event.result["RETTEXT"]);
				facade.sendNotification(HealthCheckUpFacade.LOAD_CHOICE_COMBO_LIST_FAILED, this);
			}
			CursorManager.removeBusyCursor();
		}
		
		private function choiceFault(event:FaultEvent):void
		{
			trace("Fault message in HealthCheckUpProxy: " + event.fault.faultString);
			facade.sendNotification(HealthCheckUpFacade.LOAD_CHOICE_COMBO_LIST_FAILED, this);
			CursorManager.removeBusyCursor();
		}
		
		public function saveResult(event:ResultEvent):void
		{
			if(event.result["RETCODE"]==0) {  
				facade.sendNotification(HealthCheckUpFacade.LOAD_SAVE_COMPLETE, this);
			}
			else {
				trace( "failed to load cong: (" +event.result["RETCODE"] + ") "+event.result["RETTEXT"]);
				message = event.result["RETTEXT"];
				facade.sendNotification(HealthCheckUpFacade.LOAD_SAVE_FAILED, this);
			}
			CursorManager.removeBusyCursor();
		}
		
		private function saveFault(event:FaultEvent):void
		{
			trace("Fault message in HealthCheckUpProxy: " + event.fault.faultString);
			message = event.fault["RETTEXT"];
			facade.sendNotification(HealthCheckUpFacade.LOAD_SAVE_FAILED, this);
			CursorManager.removeBusyCursor();
		}
		
		
		
		
		
		
		///////////////////////////////////////////////////////////////////////////////////////////////////
		//
		//	Data Parsing functions
		//
		///////////////////////////////////////////////////////////////////////////////////////////////////
		/**
		 * 서버에서 전달된 result 객체를 분석하여 Value Object를 생성
		 * 
		 * @param
		 * @return 건강검진조회 현황 ArrayCollection
		 */
		private function parseCongList(srcArr:ArrayCollection):ArrayCollection
		{
			var retArr:ArrayCollection = new ArrayCollection();
			
			for each(var rec:Object in srcArr) 
			{
				var vo:HealthchkreqVO = new HealthchkreqVO();
				
				vo.PERNR		= rec["PERNR"];			//사원 번호
				vo.MEDCK		= rec["MEDCK"];			//검진종류
				vo.STCD2		= rec["STCD2"];			//병원
				vo.ZYEAR		= rec["ZYEAR"];			//회계연도
				
				vo.INDAT		= rec["INDAT"];			//신청일
				vo.MREDAT		= rec["MREDAT"];		//검진예약일
				vo.MEDDT		= rec["MEDDT"];			//검진일
				
				vo._INDAT_F		= DateUtil.toDefaultString(vo.INDAT);		//신청일
				vo._MREDAT_F	= DateUtil.toDefaultString(vo.MREDAT);		//검진예약일
				vo._MEDDT_F		= DateUtil.toDefaultString(vo.MEDDT);		//검진일
				
				vo.MEDCD		= rec["MEDCD"];			//선택진료항목
				vo.MEDLV		= rec["MEDLV"];			//건강검진 결과 등급
				vo.MEDCKT		= rec["MEDCKT"];		//검진종류명
				vo.COMNM		= rec["COMNM"];			//업체명
				vo.MEDTX		= rec["MEDTX"];			//선택진료항목명
				vo.MEDLVT		= rec["MEDLVT"];		//검진결과 등급
				vo.OPINION		= "";
				retArr.addItem(vo);
			}
			return retArr;	
		}
		
		private function parseOpinion(srcArr:ArrayCollection):void
		{
			for each(var rec:Object in srcArr) 
			{
				var vo:HealthchkreqVO = findYearData(rec["ZYEAR"]);
				if(vo!=null) {
					vo.OPINION += rec["VDATA"] + "\n";
				}
			}
		}
		
		private function findYearData(year:Number):HealthchkreqVO
		{
			for each(var vo:HealthchkreqVO in dataList) {
				if(vo.ZYEAR == year) {
					return vo;
				}
			}
			return null;	
		}
		
		private function parseHospitalList(srcArr:ArrayCollection):ArrayCollection
		{
			var retArr:ArrayCollection = new ArrayCollection();
			
			for each(var rec:Object in srcArr)
			{
				var vo:HospitalVO = new HospitalVO();
				
				vo.STCD2	= rec["STCD2"];
				vo.COMNM	= rec["COMNM"];
				retArr.addItem(vo);
			}
			
			return retArr;
		}
		
		private function parseHospitalDefault(srcArr:ArrayCollection):ArrayCollection
		{
			var retArr:ArrayCollection = new ArrayCollection();
			
			for each(var rec:Object in srcArr)
			{
				var vo:HospitalDefaultVO = new HospitalDefaultVO();
				
				vo.STCD2	= rec["STCD2"];
				vo.COMNM	= rec["COMNM"];
				
				vo.STCD2		= rec["STCD2"];			//병원
				vo.COMNM		= rec["COMNM"];			//업체명
				vo.ZIPCO		= rec["ZIPCO"];			//우편번호
				vo.ADDR			= rec["ADDR"];			//주소
				vo.TELNM		= rec["TELNM"];			//대표자 Office Tel.
				vo.NOTICE		= rec["NOTICE"];		//특징
				vo.HOME_URL		= rec["HOME_URL"];		//TEXT
				
				retArr.addItem(vo);
			}
			
			return retArr;
		}
		
		private function parseHospitalDefaultList(srcArr:ArrayCollection):ArrayCollection
		{
			var retArr:ArrayCollection = new ArrayCollection();
			monthData = new ArrayCollection();
			var month:int;
			
			for each(var rec:Object in srcArr)
			{
				var vo:HospitalDefaultListVO = new HospitalDefaultListVO();
				
				vo.MONTH		= rec["MONTH"];			//월
				//vo.REDAT		= DateUtil.toDefaultString(rec["REDAT"]);			//예약가능일
				vo.REDAT		= rec["REDAT"];			//예약가능일
				vo.RECNT		= rec["RECNT"];			//예약가능 인원수
				vo.RECNT2		= rec["RECNT2"];		//예약인원수
				vo.RECNT3		= rec["RECNT3"];		//잔여인원수
				
				retArr.addItem(vo);
			}
			
			monthData.addItem({label:ResourceManager.getInstance().getString("HealthCheckUp", "SELECT"), data:0});
			
			for(var i:int = 0; i < srcArr.length; i++) {
				
				var obj:Object = new Object();
				month = 0;
				
				if(monthData.length != 0) {
					for(var ii:int = 0; ii < monthData.length; ii++) {
						if(monthData[ii].data == srcArr[i].MONTH) {
							month++;
						}
					}
				} else {
					//monthData.addItem({label:srcArr[i].MONTH, data:srcArr[i].MONTH});
				}
				if(month == 0) {
					monthData.addItem({label:srcArr[i].MONTH, data:srcArr[i].MONTH});
				}
			}
			return retArr;
		}
		
		private function parseChoiceCombo(srcArr:ArrayCollection):ArrayCollection
		{
			var retArr:ArrayCollection = new ArrayCollection();
			
			retArr.addItem({label:ResourceManager.getInstance().getString("HealthCheckUp", "SELECT"), data:0});
			
			for each(var rec:Object in srcArr) 
			{
				//선택진료항목, 선택진료항목명
				retArr.addItem({label:rec["MEDTX"], data:rec["MEDCD"]});
			}
			return retArr;
		}
	}
}